package com.qhhc.service.impl;

import com.qhhc.entity.CustomerServiceSession;
import com.qhhc.mapper.CustomerServiceSessionMapper;
import com.qhhc.service.CustomerServiceSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 客服会话服务实现类
 */
@Service
public class CustomerServiceSessionServiceImpl implements CustomerServiceSessionService {

    @Autowired
    private CustomerServiceSessionMapper customerServiceSessionMapper;

    /**
     * 创建会话
     *
     * @param session 会话信息
     * @return 会话ID
     */
    @Override
    public String createSession(CustomerServiceSession session) {
        // 生成会话ID
        if (session.getSessionId() == null || session.getSessionId().isEmpty()) {
            session.setSessionId(UUID.randomUUID().toString().replace("-", ""));
        }
        
        // 设置默认状态
        if (session.getStatus() == null) {
            session.setStatus(0); // 进行中
        }
        
        // 设置创建时间和更新时间
        if (session.getCreateTime() == null) {
            session.setCreateTime(new Date());
        }
        session.setUpdateTime(new Date());
        
        customerServiceSessionMapper.insertSession(session);
        return session.getSessionId();
    }

    /**
     * 获取会话信息
     *
     * @param sessionId 会话ID
     * @return 会话信息
     */
    @Override
    public CustomerServiceSession getSessionBySessionId(String sessionId) {
        return customerServiceSessionMapper.selectSessionBySessionId(sessionId);
    }

    /**
     * 获取用户会话列表
     *
     * @param userId 用户ID
     * @return 会话列表
     */
    @Override
    public List<CustomerServiceSession> getSessionsByUserId(Long userId) {
        return customerServiceSessionMapper.selectSessionsByUserId(userId);
    }

    /**
     * 获取访客会话列表
     *
     * @param visitorId 访客ID
     * @return 会话列表
     */
    @Override
    public List<CustomerServiceSession> getSessionsByVisitorId(String visitorId) {
        return customerServiceSessionMapper.selectSessionsByVisitorId(visitorId);
    }

    /**
     * 获取客服会话列表
     *
     * @param serviceId 客服ID
     * @return 会话列表
     */
    @Override
    public List<CustomerServiceSession> getSessionsByServiceId(Long serviceId) {
        return customerServiceSessionMapper.selectSessionsByServiceId(serviceId);
    }

    /**
     * 更新会话状态
     *
     * @param sessionId 会话ID
     * @param status    状态
     * @return 结果
     */
    @Override
    public int updateSessionStatus(String sessionId, Integer status) {
        return customerServiceSessionMapper.updateSessionStatus(sessionId, status);
    }

    /**
     * 会话评价
     *
     * @param sessionId 会话ID
     * @param score     评分
     * @param content   评价内容
     * @return 结果
     */
    @Override
    public int evaluateSession(String sessionId, Integer score, String content) {
        return customerServiceSessionMapper.updateSessionEvaluation(sessionId, score, content);
    }

    /**
     * 更新会话最后一条消息
     *
     * @param sessionId   会话ID
     * @param lastMessage 最后一条消息
     * @return 结果
     */
    @Override
    public int updateSessionLastMessage(String sessionId, String lastMessage) {
        return customerServiceSessionMapper.updateSessionLastMessage(sessionId, lastMessage);
    }

    /**
     * 分页获取会话列表
     *
     * @param params 查询参数
     * @return 会话列表
     */
    @Override
    public List<CustomerServiceSession> getSessionList(Map<String, Object> params) {
        return customerServiceSessionMapper.selectSessionList(params);
    }

    /**
     * 获取会话数量
     *
     * @param params 查询参数
     * @return 会话数量
     */
    @Override
    public int getSessionCount(Map<String, Object> params) {
        return customerServiceSessionMapper.selectSessionCount(params);
    }
} 